package com.example.studyflow.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @Author: hanll
 * @ClassName: ThreadTest
 * @Date: 2022-10-26 11:46
 * @Version: 1.0
 * @Description:
 */
class ThreadRush {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        cell cell = new cell();
        FutureTask<String> futureTask = new FutureTask<>(cell);
        new Thread(futureTask,"小明").start();
        new Thread(futureTask,"小红").start();
        new Thread(futureTask,"小明").start();
        System.out.println(futureTask.get());
    }
}

class cell implements Callable<String> {

    private boolean isok = false;

    @Override
    public String call() throws Exception {
        synchronized (this) {
            if (isok == false) {
                isok = true;
                return Thread.currentThread().getName() + "抢答成功";
            } else {
                return Thread.currentThread().getName() + "抢答失败";
            }
        }
    }
}

